﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    // NOTE: If you change the class name "EditInsBook" here, you must also update the reference to "EditInsBook" in App.config.
    public partial class MainService : IDoiSo
    {
        public DataSet HRM_Doiso_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_DoiSo_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        

        public DataSet HRM_Doiso_GetList(out int ItemCount, int _month, int _year, int branchID, int departmentID,int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_DoiSo_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@DepartmentID", departmentID);
            dbCmd.Parameters.AddWithValue("@BranchID", branchID);            
            dbCmd.Parameters.AddWithValue("@month", _month);
            dbCmd.Parameters.AddWithValue("@year", _year);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public HRM_DoisoInfo HRM_Doiso_GetInfo(int HRM_DoiSoID)
        {
            HRM_DoisoInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_DoiSo_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@HRM_DoiSo_ID", HRM_DoiSoID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new HRM_DoisoInfo();

                    retVal.doiso_ID = Convert.ToInt32(dr["doiso_ID"]);
                    retVal.doiso_branchID = Convert.ToInt32(dr["doiso_branchID"]);
                    retVal.doiso_employeeID = Convert.ToInt32(dr["doiso_employeeID"]);
                    retVal.doiso_loai = Convert.ToString(dr["doiso_loai"]);
                    retVal.doiso_ngaynhap = dr["doiso_ngaynhap"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["doiso_ngaynhap"]);
                    retVal.doiso_sosotam = dr["doiso_sosotam"].ToString();
                    retVal.doiso_sosodoi = dr["doiso_sosodoi"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int HRM_Doiso_Insert(HRM_DoisoInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_DoiSo_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@doiso_branchID", eInfo.doiso_branchID);
            dbCmd.Parameters.AddWithValue("@doiso_employeeID", eInfo.doiso_employeeID);
            dbCmd.Parameters.AddWithValue("@doiso_loai", eInfo.doiso_loai);
            dbCmd.Parameters.AddWithValue("@doiso_sosotam", eInfo.doiso_sosotam);
            dbCmd.Parameters.AddWithValue("@doiso_sosodoi", eInfo.doiso_sosodoi);
            if (eInfo.doiso_ngaynhap != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@doiso_ngaynhap", eInfo.doiso_ngaynhap);
            else
                dbCmd.Parameters.AddWithValue("@doiso_ngaynhap", DBNull.Value);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool HRM_Doiso_Update(HRM_DoisoInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_DoiSo_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@doiso_ID", eInfo.doiso_ID);
            dbCmd.Parameters.AddWithValue("@doiso_branchID", eInfo.doiso_branchID);
            dbCmd.Parameters.AddWithValue("@doiso_employeeID", eInfo.doiso_employeeID);
            dbCmd.Parameters.AddWithValue("@doiso_loai", eInfo.doiso_loai);
            dbCmd.Parameters.AddWithValue("@doiso_sosotam", eInfo.doiso_sosotam);
            dbCmd.Parameters.AddWithValue("@doiso_sosodoi", eInfo.doiso_sosodoi);
            if (eInfo.doiso_ngaynhap != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@doiso_ngaynhap", eInfo.doiso_ngaynhap);
            else
                dbCmd.Parameters.AddWithValue("@doiso_ngaynhap", DBNull.Value);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool HRM_Doiso_Delete(int EditInsBookID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_DoiSo_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@doiso_ID", EditInsBookID);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool HRM_DoiSo_DeleteStringID(string strHRM_DoisoEmployeeID, string LoaiDoiSo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_DoiSo_DeleteStringID", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@strHRM_DoisoEmployeeID", strHRM_DoisoEmployeeID);
            dbCmd.Parameters.AddWithValue("@LoaiDoiSo", LoaiDoiSo);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        
        public DataSet HRM_Doiso_Excel(DateTime fromDate, DateTime toDate,int _branchID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_DoiSo_Excel", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@FromDate", fromDate);
            dbCmd.Parameters.AddWithValue("@ToDate", toDate);
            dbCmd.Parameters.AddWithValue("@BranchID", _branchID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool HRM_Doiso_CapNhatDeNghiCapSo(int BranchID, string LoaiDoiSo, string strEmployeeID, string SoSoDauDS,DateTime toDate)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Doiso_CapNhatDeNghiCapSo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@BranchID", BranchID);
            dbCmd.Parameters.AddWithValue("@LoaiDoiSo", LoaiDoiSo);
            dbCmd.Parameters.AddWithValue("@strEmployeeID", strEmployeeID);
            dbCmd.Parameters.AddWithValue("@SoSoDauDS", SoSoDauDS);
            
            if(toDate == DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@CapDenNgay", DBNull.Value);
            else
                dbCmd.Parameters.AddWithValue("@CapDenNgay", toDate);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}

